package com.ple.app.map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.ags.ArcGISImageServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.Envelope;

import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.ple.app.util.SoilProperties;
import com.ple.app.jzsf.R;
import com.ple.app.util.GetSnapShot;

/**
 * Created by twenj on 2016/11/15.
 */

public class MapActivity extends Activity {

    private static int soilInfo;

    private TextView textViewChooseArea;
    private Spinner spinnerSoilProperties;
    private ArrayAdapter<String> adapterSpecificSoil;
    private Button btnShareSpecificMap;

    private MapView mapView;
    private ArcGISImageServiceLayer imageServiceLayer;
    private ArcGISTiledMapServiceLayer tiledMapServiceLayer;
    private ArcGISFeatureLayer featureLayer;
    private ArcGISDynamicMapServiceLayer dynamicMapServiceLayer;
    private GraphicsLayer graphicsLayer;
    private String servicesURL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_tabs);

        servicesURL = "http://192.168.13.99:6080/arcgis/rest/services/";

        textViewChooseArea = (TextView) findViewById(R.id.map_textView_chooseArea);
        spinnerSoilProperties = (Spinner) findViewById(R.id.map_spinner_SpecificSoil);
        btnShareSpecificMap = (Button) findViewById(R.id.btn_share_specificMap);
        mapView = (MapView) findViewById(R.id.map_specificMap);
        //设定显示边界
        Envelope env = new Envelope(178057.107,3787694.999,253715,3837921);
        mapView.setMaxExtent(env);

        adapterSpecificSoil = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SoilProperties.SOILSPECIALINFO);
        spinnerSoilProperties.setAdapter(adapterSpecificSoil);

        /**
         * 监听下拉框的选择状态
         */
        spinnerSoilProperties.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                soilInfo = position;
                featureLayer = null;
                //Toast.makeText(MapActivity.this,SoilProperties.SOILSPECIALINFO[position]+"",Toast.LENGTH_LONG).show();
                if (position == 0) {
                    //imageServiceLayer = new ArcGISImageServiceLayer(servicesURL+"wg_nocache/MapServer/0", null);
                    tiledMapServiceLayer = new ArcGISTiledMapServiceLayer(servicesURL + "wg/MapServer");
                    //mapView.zoomTo(new Point(3827072,214980),1);
                    //dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(servicesURL+"wg_nocache/MapServer/0");
                    //featureLayer = new ArcGISFeatureLayer(servicesURL + "wg_nocache/MapServer/0", ArcGISFeatureLayer.MODE.SNAPSHOT);
                } else {
                    //imageServiceLayer = new ArcGISImageServiceLayer(servicesURL+"ff_nocache/MapServer/0",null);
                    tiledMapServiceLayer = new ArcGISTiledMapServiceLayer(servicesURL + "ff/MapServer");
                    //mapView.zoomTo(new Point(3806835,200651),1);
                    //dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(servicesURL+"ff_nocache/MapServer/0");
                    //featureLayer = new ArcGISFeatureLayer(servicesURL + "ff_nocache/MapServer/0", ArcGISFeatureLayer.MODE.SNAPSHOT);
                }
                mapView.removeAll();
                mapView.setMapBackground(Color.parseColor("#ffffff"), Color.TRANSPARENT, 0, 0);
                mapView.addLayer(tiledMapServiceLayer);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //跳转到地块选择界面
        textViewChooseArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MapActivity.this, ChooseAreaActivity.class);
                startActivity(intent);
            }
        });

        /**
         * 地图截图与分享
         * 保存到微信 ，同时存储到SD卡
         */
        btnShareSpecificMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GetSnapShot snapShot = new GetSnapShot(MapActivity.this,mapView);
                snapShot.shareViewToWeChat();//保存截图到微信
                snapShot.saveViewShotToNative();//保存截图到SD卡
                Toast.makeText(MapActivity.this,"保存成功,路径为："+snapShot.getFilepath(),Toast.LENGTH_LONG).show();
            }
        });
    }

    //重新启动的时候根据拿到的值更新界面
    @Override
    protected void onResume() {
        super.onResume();
        //获取到选择的结果
        if (getIntent().getStringExtra("result") != null) {
            textViewChooseArea.setText(getIntent().getStringExtra("result"));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /***
     * MapActivity启动采用的是singleTask方式，当该intent重启的时候不会再调用onCreat方法，
     * 而是调用onNewIntent方法，这样就是得MapActivity通过getIntent()方法获取数据的时候不
     * 是原先旧的Intent，而是包含了新值的Intent
     * @param intent
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
}
